package com.quzhi.sys.controller.UserController;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.quzhi.common.Result;
import com.quzhi.common.code.Code;
import com.quzhi.sys.dto.MenuDto;
import com.quzhi.sys.entity.Mata;
import com.quzhi.sys.entity.Menu;
import com.quzhi.sys.entity.Role;
import com.quzhi.sys.entity.RoleMenu;
import com.quzhi.sys.service.impl.MenuServiceImpl;
import com.quzhi.sys.service.impl.RoleMenuServiceImpl;
import com.quzhi.sys.service.impl.RoleServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.util.*;

/**
 * <p>
 * 前端控制器 菜单控制器
 * </p>
 *
 * @author Xibing
 * @since 2024-08-18
 */
@RestController
@RequestMapping("/sys/menu")
public class MenuController {


    @Autowired
    private MenuServiceImpl menuService;


    @Autowired
    private RoleServiceImpl roleService;


    @Autowired
    private RoleMenuServiceImpl roleMenuService;

    // 返回所有菜单，
    @GetMapping("/list")
    public Result<?> getAllMenu() {
        // 查询所有 父菜单(本项目所有的菜单只有两级，所以只考虑两级菜单)
        LambdaQueryWrapper<Menu> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByAsc(Menu::getId);
        wrapper.isNull(Menu::getParentId);

        List<Menu> parentMenu = menuService.list(wrapper);

        // 获取所有子菜单
        List<MenuDto> menuDtos = new ArrayList<>();

        for (Menu menu : parentMenu) {
            MenuDto menuDto = new MenuDto();

            BeanUtils.copyProperties(menu, menuDto);

            // 设置mata
            Mata mata = new Mata();
            mata.setTitle(menu.getNameZh());
            mata.setIcon(menu.getIconCls());

            menuDto.setMate(mata);

            // 设置子组件，
            LambdaQueryWrapper<Menu> menuLambdaQueryWrapper = new LambdaQueryWrapper<>();
            menuLambdaQueryWrapper.eq(Menu::getParentId, menu.getId());
            menuLambdaQueryWrapper.orderByAsc(Menu::getId);
            List<Menu> children = menuService.list(menuLambdaQueryWrapper);
            List<MenuDto> childrenMueuDtos = new ArrayList<>();
            for (Menu child : children) {
                MenuDto childrenMueu = new MenuDto();
                BeanUtils.copyProperties(child, childrenMueu);

                // 设置mata
                Mata chilrenMata = new Mata();
                chilrenMata.setTitle(child.getNameZh());
                chilrenMata.setIcon(child.getIconCls());

                childrenMueu.setMate(chilrenMata);
                childrenMueuDtos.add(childrenMueu);

            }

            menuDto.setChildren(childrenMueuDtos);


            menuDtos.add(menuDto);
        }

        return Result.success(menuDtos);
    }


    // 返回权限对应的路由,
    @PostMapping("/getByRoleName")
    public Result<?> getMenuByRoleName(@RequestBody List<String> roleNames) {
        // 先更具角色名称查询角色Id

        if (roleNames == null || roleNames.size() < 1) return Result.fail(Code.UPDATE_FAIL_CODE, "数据有误");


        Set<String> menuIds = new HashSet<>();  // 所有菜单的Id

        for (String roleName : roleNames) {
            // 如果是超级管理员，则返回所有路由
            if (roleName.equals("admin")) {
                LambdaQueryWrapper<Menu> lambdaQueryWrapper = new LambdaQueryWrapper<>();
                lambdaQueryWrapper.orderByAsc(Menu::getId);
                List<Menu> menuList = menuService.list(lambdaQueryWrapper);
                for (Menu menu : menuList) {
                    menuIds.add(menu.getId());
                }

            } else {

                LambdaQueryWrapper<Role> roleLambdaQueryWrapper = new LambdaQueryWrapper<>();
                roleLambdaQueryWrapper.eq(Role::getRoleName, roleName);
                Role roleServiceOne = roleService.getOne(roleLambdaQueryWrapper);

                if (roleServiceOne == null) continue;

                // 更具角色id查询对应的路由ID
                LambdaQueryWrapper<RoleMenu> roleMenuLambdaQueryWrapper = new LambdaQueryWrapper<>();
                roleMenuLambdaQueryWrapper.eq(RoleMenu::getRoleId, roleServiceOne.getRoleId());
                // 所有选项对应的菜单Id
                // 排序
                roleMenuLambdaQueryWrapper.orderByAsc(RoleMenu::getMenuId);
                List<RoleMenu> roleMenus = roleMenuService.list(roleMenuLambdaQueryWrapper);

                // 对菜单id进行去重
                for (RoleMenu roleMenu : roleMenus) {
                    menuIds.add(roleMenu.getMenuId());
                }
            }

        }

        // 排序
        // 使用自定义比较器按数字大小排序
        // 使用自定义比较器进行排序


        // 将 Set 转换为 List
        List<String> numberList = new ArrayList<>(menuIds);

        // 使用自定义比较器对 List 进行排序
        numberList.sort((a, b) -> Integer.compare(Integer.parseInt(a), Integer.parseInt(b)));


        // 获取所有菜单
        List<Menu> menuList = new ArrayList<>();  // 所有菜单集合

        for (String menuId : numberList) {
            Menu menu = menuService.getById(menuId);
            menuList.add(menu);
        }

        // 删除所有父菜单
        // 获取父组件
        // 获取子菜单的父菜单
        menuList.removeIf(menu -> menu.getParentId() == null);

        Set<String> parentId = new HashSet<>();
        for (Menu menu : menuList) {
            parentId.add(menu.getParentId());
        }

        List<String> parentIds = new ArrayList<>(parentId);

        // 使用自定义比较器对 List 进行排序
        parentIds.sort((a, b) -> Integer.compare(Integer.parseInt(a), Integer.parseInt(b)));

        // 获取所有父
        List<Menu> parentMenu = new ArrayList<>();
        for (String pId : parentIds) {
            Menu byId = menuService.getById(pId);
            parentMenu.add(byId);
        }

        List<MenuDto> menuDtos = new ArrayList<>();
        for (Menu menu : parentMenu) {
            MenuDto parentDto = new MenuDto();
            BeanUtils.copyProperties(menu, parentDto);

            Mata mata = new Mata();
            mata.setTitle(menu.getNameZh());
            mata.setIcon(menu.getIconCls());

            parentDto.setMate(mata);

            // 获取子菜单
            List<MenuDto> childrenDtos = new ArrayList<>();
            for (Menu childrenMenu : menuList) {
                MenuDto childrenDto = new MenuDto();
                BeanUtils.copyProperties(childrenMenu, childrenDto);

                Mata childrenMata = new Mata();
                childrenMata.setTitle(childrenMenu.getNameZh());
                childrenMata.setIcon(childrenMenu.getIconCls());

                childrenDto.setMate(childrenMata);


                // 向父菜单添加子菜单
                if (childrenDto.getParentId().equals(menu.getId())) {
                    childrenDtos.add(childrenDto);
                }
            }

            parentDto.setChildren(childrenDtos);
            menuDtos.add(parentDto);
        }


        return Result.success(menuDtos);
    }


}
